<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");
require_once($root."functions/common/common.php");
require_once($root."functions/general_function.php");
require_once($root."functions/downline/downline_function.php");
require_once($root."functions/comm/comm_function.php");
require_once($root."initialize.php");

//if(1){//ndate("d")=="01" && ndate("H")=="00"){
//if(strtotime(ndate('Y-m-d')) <= strtotime('2009-06-21')){
if(1){
	if(($db_msg=cron_initialize()) !== true){
		$errmsg="Error starting Plan A Calculation due to database error.<br />\n<br />\n$db_msg";
		$a_sub="$vars[title] - Plan A Calculation Failed";
		$a_msg=
		"<p>Dear admin,</p>
		$errmsg";
		email_admin($a_sub, $a_msg, 'e');
	}else{
		$db=$vars["db"];
		$time=mktime(0, 0, 0 , ndate('n'), ndate('j')-1, ndate("Y"));
		//$time=mktime(0, 0, 0 , 11, 24, 2009);
		$day=date('j', $time);
		$month=date('n', $time);
		$year=date('Y', $time);
		$time_start = mktime(0, 0, 0 , $month, $day, $year);
		

		//####################placement####################
		$comm_title = "Placement";
		$comm_code = "m";
		$comm_date=date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));

		//executed = check cron_table if exist with type = placement, day, month, year
		if(!($r=mysql_query("select count(*) from $db->cron where type='$comm_code' and year='$year' and month='$month' and day='$day';"))){
			$errmsg[$comm_code]="Error starting $comm_title Calculation [$comm_date] due to database error.<br />\n<br />\n".mysql_error();
		}else{
			if(($executed[$comm_code] = @mysql_result($r, 0)) === false){
				$errmsg[$comm_code]="Error starting $comm_title Calculation [$comm_date] due to database error.<br />\n<br />\n".mysql_error();
			}
		}

		if(!$errmsg[$comm_code]){
			if (!$executed[$comm_code]){
				//insert into cron_table type = placement, started = y, year, month, day, cron start date = datetime
				$sql = "insert into $db->cron (type, started, completed, success, year, month, day) values ('$comm_code', '".ndate($vars["system_date_format"])."', 'n', 'n', '$year', '$month', '$day')";
				if(!mysql_query($sql)){
					$errmsg[$comm_code].="Error starting $comm_title calculation [$comm_date] due to database error.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
				}else{
					//cron_id = last insert id
					$cron_id = mysql_insert_id();
					$result = auto_placement($year, $month, $day);
					$comm_errmsg_d = '';
					if($result['status'] === true){
						$success = 'y';
						$executed[$comm_code] = true;
					}else{
						$errmsg[$comm_code] = "$comm_title calculation [$comm_date] completed with errors:<br />\n<br />\n".$comm_errmsg;
						$success = 'n';
					}
					if($result['errmsg']){
						$comm_errmsg = $result['errmsg'];
						$comm_errmsg_d = mysql_real_escape_string($result['errmsg']);
					}
					$sql = "update $db->cron set completed='y', success='$success', ended='".ndate($vars["system_date_format"])."', message='$comm_errmsg_d' where id='$cron_id'";
					if(!mysql_query($sql)){
						$errmsg[$comm_code]="Error completing the $comm_title calculation [$comm_date] due to database error.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n".($errmsg[$comm_code]? "<br />\n".$errmsg[$comm_code] : "");
					}
				}
			}
		}
		if ($errmsg[$comm_code]){
			$a_sub="$vars[title] - $comm_title Calculation [$comm_date] with Error";
			$a_msg=
			"<p>Dear admin,</p>
			".$errmsg[$comm_code];
			email_admin($a_sub, $a_msg, 'e');
		}

		//####################pairing & matching####################
		$comm_title = "Pairing and Matching";
		$comm_code = "p";

		$comm_date=date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
		//executed = check cron_table if exist with type = pairing & matching, day, month, year
		if(!($r=mysql_query("select count(*) from $db->cron where type='$comm_code' and year='$year' and month='$month' and day='$day'"))){
			$errmsg[$comm_code][$i]="Error starting $comm_title Calculation [$comm_date] due to database error.<br />\n<br />\n".mysql_error();
		}else{
			if(($executed[$comm_code] = @mysql_result($r, 0)) === false){
				$errmsg[$comm_code][$i]="Error starting $comm_title Calculation [$comm_date] due to database error.<br />\n<br />\n".mysql_error();
			}
		}

		if(!$errmsg[$comm_code]){
			if($executed['m']){//auto placement must 1st be executed
				if (!$executed[$comm_code]){
					//insert into cron_table type = pairing & matching, started = y, year, month, day, cron start date = datetime
					$sql = "insert into $db->cron (type, started, completed, success, year, month, day) values ('$comm_code', '".ndate($vars["system_date_format"])."', 'n', 'n', '$year', '$month', '$day')";
					if(!mysql_query($sql)){
						$errmsg[$comm_code].="Error starting $comm_title calculation [$comm_date] due to database error.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
					}else{
						//cron_id = last insert id
						$cron_id = mysql_insert_id();
						$result = calc_pairing_matching($year, $month, $day);
						$comm_errmsg_d = '';
						if($result['status'] === true){
							$success = 'y';
							$executed[$comm_code] = true;
						}else{
							$errmsg[$comm_code] = "$comm_title calculation [$comm_date] completed with errors:<br />\n<br />\n".$comm_errmsg;
							$success = 'n';
						}
						if($result['errmsg']){
							$comm_errmsg = $result['errmsg'];
							$comm_errmsg_d = mysql_real_escape_string($result['errmsg']);
						}
						$sql = "update $db->cron set completed='y', success='$success', ended='".ndate($vars["system_date_format"])."', message='$comm_errmsg_d' where id='$cron_id'";
						if(!mysql_query($sql)){
							$errmsg[$comm_code]="Error completing the $comm_title calculation [$comm_date] due to database error.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n".($errmsg[$comm_code]? "<br />\n".$errmsg[$comm_code] : "");
						}
					}
				}
			}else{
				$errmsg[$comm_code]="Error starting $comm_title calculation [$comm_date] due to auto-placement not completed.<br />\n";
			}
		}
		if ($errmsg[$comm_code]){
			$a_sub="$vars[title] - $comm_title Calculation [$comm_date] with Error";
			$a_msg=
			"<p>Dear admin,</p>
			".$errmsg[$comm_code];
			email_admin($a_sub, $a_msg, 'e');
		}
	}
}
?>